package filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.HttpServletRequest;

import java.awt.*;
import java.io.IOException;

@WebFilter(filterName = "ImgFilter")
public class ImgFilter implements Filter {
    private FilterConfig filterConfig = null;

    public void init(FilterConfig config) {
        this.filterConfig = config;
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        filterConfig.getServletContext().log("水印过滤器");
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String filePath = httpServletRequest.getServletPath();
        System.out.println("------>URL:" + filePath);
        String fileExt = filePath.substring(filePath.indexOf(".") + 1);
        System.out.println("------>Ext Name:" + fileExt);
        String fileRealPath = filterConfig.getServletContext().getRealPath(filePath);
        System.out.println("------>Real URL:" + fileRealPath);
        if ("jpg".equals(fileExt)) {
            watermark.pressText("水印测试", fileRealPath, "黑体", Font.BOLD, Color.red.getRGB(), 25, 90, 10, response.getOutputStream());
        }
        chain.doFilter(request, response);
    }
}
